import {createRouter, createWebHistory, RouteRecordRaw} from 'vue-router'
import {useHead} from "@vueuse/head";
import {description, keywords, title} from "@/config/seo";
import {closeGlobalLoading, showNavbar} from "@/utils/common";
import {ElMessage} from "element-plus";
import LoginDialog from "@/components/LoginDialog/LoginDialog";


const routes: Array<RouteRecordRaw> = [
    {
        path: '/',
        name: 'home',
        component: () => import('../views/HomeView.vue'),
    },
    {
        path: '/userinfo',
        name: 'user_info',
        meta: {
            title: "填写个人信息",
            requireLogin: true,
        },
        component: () => import( '../views/UserInfoView.vue')
    },
    {
        path: '/resume_create',
        name: 'resume_create',
        meta: {
            title: "AI简历生成",
            requireLogin: true,
        },
        component: () => import( '../views/ResumeCreate.vue')
    },
    {
        path: '/person',
        name: 'PersonalCenter',
        component: () => import( '../views/person/PersonalCenter.vue'),
        meta: {
            title: '个人中心',
            requireLogin: true,
        },
        redirect: "/person/information",
        children: [
            {
                path: '/person/information',
                name: 'Information',
                component: () => import( '../views/person/PersonalInformation.vue'),
            }, {
                path: '/person/setting',
                name: 'PersonalSetting',
                component: () => import( '../views/person/PersonalSetting.vue'),
            }, {
                path: '/person/mResume',
                name: 'MResume',
                component: () => import( '../views/person/PersonalResume.vue'),
            }, {
                path: '/person/message',
                name: 'Message',
                component: () => import( '../views/person/PersonalMessage.vue'),
            }
        ]

    },
    {
        path: '/debug',
        name: 'Debug',
        component: () => import( '../views/test.vue')
    },
    {
        path: '/resume',
        name: 'resume',
        meta: {
            title: "模板大全",
            requireLogin: false,
        },
        component: () => import( '../views/resumeList/index.vue')
    },
    {
        path: '/designResume/:id',
        name: 'designer',
        meta: {
            title: '设计简历',
            keepAlive: true,
            requireLogin: true,
        },
        component: () => import( '../views/designerResume/index.vue')
    },
    {
        path: '/interview',
        name: 'Interview',
        redirect: '/interview/cover',
        component: () => import('../views/interview/interviewSimulate.vue'),
        children: [
            {
                path: '/interview/cover',
                name: 'InterviewCover',
                meta: {
                    requireLogin: false,
                },
                component: () => import('../views/interview/interviewCover.vue')
            },
            {
                path: '/interview/reqChoose',
                name: 'InterviewReqChoose',
                meta: {
                    requireLogin: true,
                },
                component: () => import('../views/interview/interviewReqChoose.vue')
            },
            {
                path: '/interview/setting',
                name: 'Setting',
                redirect: '/interview/setting/requirement',
                meta: {
                    requireLogin: true,
                },
                component: () => import('../views/interview/interviewSetting.vue'),
                children: [
                    {
                        path: '/interview/setting/requirement',
                        name: 'SettingReq',
                        component: () => import('../views/interview/interviewSettingReq.vue')
                    },
                    {
                        path: '/interview/setting/service',
                        name: 'SettingService',
                        component: () => import('../views/interview/interviewSettingService.vue')
                    },
                    {
                        path: '/interview/setting/privacy',
                        name: 'SettingPrivacy',
                        component: () => import('../views/interview/interviewSettingPrivacy.vue')
                    }
                ]
            },
            {
                path: '/interview/main',
                name: 'InterviewMain',
                meta: {
                    keepAlive: true,
                    requireLogin: true,
                },
                component: () => import('../views/interview/interviewMain.vue')
            }
        ]
    },
    {
        path: '/optimize',
        name: 'optimize',
        meta: {
            title: '简历优化',
            requireLogin: true,
        },
        component: () => import( '../views/optimizeResume/optimize.vue')
    },
    {
        path: '/analyse',
        name: 'analyse',
        meta: {
            title: '简历分析',
            requireLogin: true
        },
        component: () => import( '../views/optimizeResume/analyse.vue')
    },
    {
        path: '/diagnosis',
        name: 'Diagnosis',
        redirect: '/diagnosis/cover',
        component: () => import('../views/diagnosisCenter/index.vue'),
        children: [
            {
                path: '/diagnosis/cover',
                name: 'Cover',
                component: () => import('../views/diagnosisCenter/ClinicCover.vue')
            },
            {
                path: '/diagnosis/history',
                name: 'History',
                meta: {
                    title: '面试记录',
                    requireLogin: true
                },
                component: () => import('../views/historicalRecords/index.vue')
            },
            {
                path: '/diagnosis/report/:id',
                name: 'Report',
                meta: {
                    title: '面试报告',
                    requireLogin: true
                },
                component: () => import('../views/analysisReport/index.vue')
            },
            {
                path: '/diagnosis/improve/:id',
                name: 'Improve',
                meta: {
                    title: '提升路线',
                    requireLogin: true
                },
                component: () => import('../views/improvementRoute/index.vue')
            },
            {
                path: '/diagnosis/again/:id',
                name: 'Again',
                meta: {
                    title: '面试复盘',
                    requireLogin: true
                },
                component: () => import('../views/interviewAgain/index.vue')
            }
        ]
    }
]

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes,
    scrollBehavior(to, from, savedPosition) {
        // 如果有保存的滚动位置，则滚动到该位置
        if (savedPosition) {
            return savedPosition;
        }
        // 跳转到新的页面时，滚动到顶部
        return {top: 0};
    }
})

router.beforeEach(async (to, from, next) => {
    // 设置标题、描述、关键词
    useHead({
        title: (to.meta.title as string) || title,
        meta: [
            {
                name: 'description',
                content: description
            },
            {
                name: 'keywords',
                content: keywords
            }
        ]
    });

    const token = localStorage.getItem('token');
    const userInfo = localStorage.getItem('userInfo');
    // 需要权限且未登录
    if (to.meta.requireLogin && !token) {
        closeGlobalLoading(); // 关闭全局等待层
        ElMessage.error("请先登录")
        showNavbar()
        LoginDialog('login')
        next('/')
    }
    else if (to.meta.requireLogin && token) {
        // 需要权限且已经登录
        if (userInfo) {
            // 判断该页面是否是管理员才能进入
            // const requireAdmin = to.meta.requireAdmin;
            // if (requireAdmin) {
            //     const data = await getUserPermissionAsync();
            //     const isAdmin = data.data.data;
            //     if (!isAdmin) {
            //         next('/noPermission');
            //     }
            // }

            next()
        } else {
            closeGlobalLoading(); // 关闭全局等待层
            ElMessage.error("请先登录")
            showNavbar()
            LoginDialog('login')
            next('/')
        }
    }
    else {
        next();
    }
});

export default router
